//
//  DamInfo.m
//  MapCallouts
//
//  Created by Hideo Tomizu on 10/08/17.
//  Copyright 2010 株式会社ビットウェイ. All rights reserved.
//

#import "DamInfo.h"
#import "AppDelegate.h"
#import "DetailViewController.h"

static sqlite3 *database = nil;

@implementation DamInfo

@synthesize dam_cd,
point_x ,
point_y ,
dam_name ,
water_name ,
river_name ,
type_cd ,
purpose_cd ,
height ,
width ,
volume ,
pondage ,
institution_cd ,
const_year ,
ken_cd ,
address ,
accuracy_cd,
ken_name,
type_name,
purpose_name,
institution_name,
isDirty,
isDetailViewHydrated;


+ (void) getInitialDataToDisplay:(NSString *)dbPath {
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

	if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
		
		//const char *sql = "select dam_cd, dam_name, point_x, point_y, address, dam_info.ken_cd, ken.ken_name from dam_info inner join ken on dam_info.ken_cd = ken.ken_cd";
		const char *sql = "select dam_cd, dam_name, point_x, point_y, address, dam_info.ken_cd, ken.ken_name from dam_info inner join ken on dam_info.ken_cd = ken.ken_cd where dam_info.ken_cd=13";
		
		sqlite3_stmt *selectstmt;
		
		if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL)==SQLITE_OK) {
			while(sqlite3_step(selectstmt)==SQLITE_ROW) {
				NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
				DamInfo *damInfoObj = [[DamInfo alloc] initWithPrimaryKey:primaryKey];
				
				// ダム名
				damInfoObj.dam_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
				
				// 緯度経度
				damInfoObj.point_x  = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(selectstmt, 2)];
				damInfoObj.point_y  = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(selectstmt, 3)];
				
				// 県名
				damInfoObj.ken_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
				
				// 住所（県以下）
				damInfoObj.address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
				
				damInfoObj.isDirty = NO;
				
				[appDelegate.damInfoArray addObject:damInfoObj];
				[damInfoObj release];
			}
		}
	} else {
		sqlite3_close(database);
	}
}

+ (id) getDamInfoDetail:(NSString *)dbPath damCd:(NSInteger) tmp_dam_cd{
//(id) getDamInfoDetail:(NSString *)dbPath detailViewController:(DetailViewController) detailViewController{

	if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
		
		const char *sql = "select dam_cd, dam_name, point_x, point_y, address, dam_info.ken_cd, ken.ken_name, dam_type.type_name, dam_purpose.purpose_name , dam_institution.institution_name from dam_info inner join ken on dam_info.ken_cd = ken.ken_cd inner join dam_type on dam_info.type_cd=dam_type.type_cd inner join dam_purpose on dam_info.purpose_cd=dam_purpose.purpose_cd inner join dam_institution on  dam_info.institution_cd=dam_institution.institution_cd where dam_info.dam_cd = ?";
		
		sqlite3_stmt *selectstmt;
		
		if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL)==SQLITE_OK) {
			
			sqlite3_bind_int(selectstmt, 1, tmp_dam_cd);
			
			if(sqlite3_step(selectstmt)==SQLITE_ROW) {
			
				NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
			
				DamInfo *damInfoObj = [[DamInfo alloc] initWithPrimaryKey:primaryKey];

				// ダム名
				damInfoObj.dam_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

				// 緯度経度
				damInfoObj.point_x  = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(selectstmt, 2)];
				damInfoObj.point_y  = [[NSDecimalNumber alloc] initWithDouble:sqlite3_column_double(selectstmt, 3)];

				// 県名
				damInfoObj.ken_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];

				// 住所（県以下）
				damInfoObj.address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
				
				// 形式
				damInfoObj.address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
				// 目的
				damInfoObj.address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 8)];
				// 事業者
				damInfoObj.address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 9)];
				
				damInfoObj.isDirty = NO;

				return damInfoObj;
			}
		} else {
			NSAssert1(0, @"Error while creating select statement. '%s'", sqlite3_errmsg(database));
		}
	}
	
	return NULL;
}

- (id) initWithPrimaryKey:(NSInteger)pk {
	[super init];
	dam_cd = pk;
	
	isDetailViewHydrated = NO;
	
	return self;
}

+ (void) finalizeStatements {
	if(database) sqlite3_close(database);
}

- (void) dealloc {
	
	[point_x release];
	[point_y release];
	[dam_name release];
	[water_name release];
	[river_name release];
	//[type_cd release];
	//[purpose_cd release];
	[height release];
	[width release];
	//[volume release];
	//[pondage release];
	//[institution_cd release];
	//[const_year release];
	//[ken_cd release];
	[address release];
	//[accuracy_cd release];
	
	[super dealloc];
}


@end
